package com.manor.main.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.manor.common.CommonAppConfig
import com.manor.common.http.HttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.RouteUtil
import com.manor.common.utils.ToastUtil
import com.manor.main.R
import com.manor.main.adapter.KMyGuardAdapter
import com.manor.main.bean.MyGuardBean
import com.manor.main.http.MainHttpUtil

/**
 * 守護
 */
class KMyGardFragment (var type: Int): BaseFragment() {
    internal var rcv_list: RecyclerView? = null
    internal lateinit var mAdapter: KMyGuardAdapter
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view:View = inflater.inflate(R.layout.fragment_gard, container, false)
        mInflater = LayoutInflater.from(activity)
        rcv_list = view?.findViewById(R.id.rcv_list)
        rcv_list?.setLayoutManager(LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false))
        mAdapter = KMyGuardAdapter(R.layout.item_myguard, null)
        rcv_list?.setAdapter(mAdapter)
        mAdapter?.setOnItemClickListener { adapter, view, position ->
            if (ClickUtil.canClick()) {
                val listBean: MyGuardBean = mAdapter?.getItem(position)
                if(type == 0){
                    RouteUtil.forwardUserHome(listBean.guar_id)
                }else{
                    RouteUtil.forwardUserHome(listBean.uid)
                }

            }
        }

        initMode()
        return view
    }

    private fun initMode() {
        if(type==0){
            MainHttpUtil.GetGuardList(CommonAppConfig.getInstance().uid, CommonAppConfig.getInstance().token,
                     "", object : HttpCallback() {
                override fun onSuccess(code: Int, msg: String, info: String?) {
                    if (code == 0 && info != null) {
                        if (info.isNotEmpty()) {
                            val myGuardBean = JSON.parseArray(info, MyGuardBean::class.java)
                            //   WithdrawalAccountBean withdrawalAccountBean = mGson.fromJson(info[0], WithdrawalAccountBean.class);
                            mAdapter.setNewInstance(myGuardBean)
                        } else {
                            mAdapter.setNewInstance(null)
                            mAdapter.setEmptyView(R.layout.view_no_data_dynamic4)
                        }


                    } else {
                        mAdapter.setNewInstance(null)
                        ToastUtil.show(msg)
                    }
                }
            })
        }else{
            MainHttpUtil.GetGuardList(CommonAppConfig.getInstance().uid, CommonAppConfig.getInstance().token,
                    CommonAppConfig.getInstance().uid+"", object : HttpCallback() {
                override fun onSuccess(code: Int, msg: String, info: String?) {
                    if (code == 0 && info != null) {
                        if (info.isNotEmpty()) {
                            val myGuardBean = JSON.parseArray(info, MyGuardBean::class.java)
                            //   WithdrawalAccountBean withdrawalAccountBean = mGson.fromJson(info[0], WithdrawalAccountBean.class);
                            mAdapter.setNewInstance(myGuardBean)
                        } else {
                            mAdapter.setNewInstance(null)
                            mAdapter.setEmptyView(R.layout.view_no_data_dynamic4)
                        }


                    } else {
                        mAdapter.setNewInstance(null)
                        ToastUtil.show(msg)
                    }
                }
            })
        }

    }


}
